package com.incar.chuxing.utils;

import android.content.Context;
import android.util.Base64;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class CarDamagePicLoad {


    public void load(Context context) {
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                ArrayList<String> picList = new ArrayList<>();
                picList.add("carDamageMap");
                picList.add("leftRearFender");
                picList.add("leftRearWheel");
                picList.add("leftRearDoor");
                picList.add("leftFrontDoor");
                picList.add("leftFrontWheel");
                picList.add("leftFrontFender");
                picList.add("leftRearViewMirror");
                picList.add("frontBumper");
                picList.add("rightRearViewMirror");
                picList.add("rightFrontFender");
                picList.add("rightFrontWheel");
                picList.add("rightFrontDoor");
                picList.add("rightRearDoor");
                picList.add("rightRearWheel");
                picList.add("rightRearFender");
                picList.add("rearBumper");
                picList.add("trunkLid");
                picList.add("roof");
                picList.add("frontMachineCover");


                for (String fileName : picList) {
                    readAssetsFile(context, fileName);
                }
            }
        });

    }

    private void readAssetsFile(Context context, String fileName) {
        if (!SPUtils.getInstance(context).containsKey("$s_" + fileName)) {
            InputStream inputStream = null;
            try {
                inputStream = context.getResources().getAssets().open("CarDamagePicture/" + fileName + ".png");
                byte[] b = new byte[inputStream.available()];
                inputStream.read(b);
                SPUtils.getInstance(context).putStringValue("$s_" + fileName, Base64.encodeToString(b, Base64.DEFAULT));
                Logger.d("carDamage", SPUtils.getInstance(context).getStringValue("$s_" + fileName));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
